package com.wanjia.estate.service.bdservice.zydjspireservice;

import com.wanjia.estate.pojo.entity.Identity;
import com.wanjia.estate.utils.BarcodeGeneratorUtil;
import com.wanjia.estate.utils.Base64Util;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.spire.xls.ExcelPicture;
import com.spire.xls.ExcelVersion;
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;

import java.io.File;
import java.io.IOException;

//Excel表格添加图片
@Service
public class ExcelAddImgService {

    @Value("${transparentpath}")
    private String transparent;

    public String ExcelAddPng(String excelPath, Identity identity) throws IOException {

        File file = new File(excelPath);
        String absolutePath = file.getAbsolutePath();
        String name = absolutePath.split("\\.")[0];
        BarcodeGeneratorUtil.getBarCode(identity.getSerialNumber(),name+identity.getSerialNumber()+".png");//生成条形码

        //创建Workbook实例
        Workbook workbook = new Workbook();
        //加载Excel文档
        workbook.loadFromFile(excelPath);
        //获取第一张工作表
        Worksheet sheet = workbook.getWorksheets().get(0);


        //添加图片到工作表的指定位置
        ExcelPicture pic1 = sheet.getPictures().add(2, 6,name+identity.getSerialNumber()+".png");

        if (identity.getZrfqz()!=null){
            Base64Util.stringToImage(identity.getZrfqz(), name+"qm1");//生成签名
            ExcelPicture pic2 = sheet.getPictures().add(35, 4,name+"qm1.png");
            pic2.setWidth(100);
            pic2.setHeight(30);
        }else{
            ExcelPicture pic2 = sheet.getPictures().add(35, 4,transparent);
            pic2.setWidth(100);
            pic2.setHeight(30);
        }

        if (identity.getSrfqz()!=null){
            Base64Util.stringToImage(identity.getSrfqz(), name+"qm2");//生成签名
            ExcelPicture pic3 = sheet.getPictures().add(35, 6,name+"qm2.png");
            pic3.setWidth(100);
            pic3.setHeight(30);
        }else {
            ExcelPicture pic3 = sheet.getPictures().add(35, 6,transparent);
            pic3.setWidth(100);
            pic3.setHeight(30);
        }

        if (identity.getDlrqz()!=null){
            Base64Util.stringToImage(identity.getDlrqz(), name+"qm3");//生成签名
//            ExcelPicture pic4 = sheet.getPictures().add(37, 4,name+"qm3.png");
            ExcelPicture pic5 = sheet.getPictures().add(37, 6,name+"qm3.png");
//            pic4.setWidth(100);
//            pic4.setHeight(30);
            pic5.setWidth(100);
            pic5.setHeight(30);
        }else{
            ExcelPicture pic4 = sheet.getPictures().add(37, 4,transparent);
            ExcelPicture pic5 = sheet.getPictures().add(37, 6,transparent);
            pic4.setWidth(100);
            pic4.setHeight(30);
            pic5.setWidth(100);
            pic5.setHeight(30);
        }


        //设置图片的宽度和高度
        pic1.setWidth(200);
        pic1.setHeight(40);


        //保存文档
        workbook.saveToFile(excelPath, ExcelVersion.Version2013);

        return absolutePath;
    }
}
